﻿namespace VectorPicsEditor.Controls
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Forms;
    using System.Windows.Shapes;

    public partial class PropertiesBar : UserControl
    {
        public CommonProperties Properties;
        public List<ShapeWrapper> Shapes;

        public event EventHandler SelectedShapeChanged;

        public PropertiesBar()
        {
            this.InitializeComponent();

            this.shapesComboBox.SelectedIndexChanged += this.selectedShapeChanged;
            this.Shapes = new List<ShapeWrapper>();
        }

        public void AddShape(ShapeWrapper shape)
        {
            this.Shapes.Add(shape);
            this.shapesComboBox.Items.Add(shape.Name);
            this.shapesComboBox.SelectedItem = shape.Name;
            this.SelectShape(shape);
        }

        public void SelectShape(ShapeWrapper shape)
        {
            if (this.Properties != null)
            {
                this.splitContainer.Panel1.Controls.Remove(this.Properties);
            }

            if(shape.Shape is Line)
            {
                this.Properties = new LineProperties();
            }
            else if (shape.Shape is Rectangle && shape.Shape.Name == "")//settings shape
            {
                this.Properties = new PreviewProperties();
            }
            else if (shape.Shape is System.Windows.Controls.Image)
            {
                this.Properties = new ImageProperties();
            }

            else if (shape.Shape is Rectangle)
            {
                this.Properties = new RectangleProperties();
            }

            else if (shape.Shape is Ellipse)
            {
                this.Properties = new EllipseProperties();
            }

            else if (shape.Shape is System.Windows.Controls.TextBlock)
            {
                this.Properties = new TextProperties();
            }

            this.Properties.Initialize(shape, this.shapesComboBox);
            this.Properties.AutoSizeMode = AutoSizeMode.GrowAndShrink;
            this.Properties.AutoSize = true;
            this.Properties.Dock = DockStyle.Top;
            this.splitContainer.Panel1.Controls.Add(this.Properties);
            this.shapesComboBox.SelectedItem = shape.Name;
        }

        private void selectedShapeChanged(object sender, EventArgs e)
        {
            if (this.SelectedShapeChanged != null)
            {
                this.SelectedShapeChanged(
                                          this.Shapes.Where(s => s.Name == this.shapesComboBox.SelectedItem.ToString())
                                              .First(),
                                          null);
            }
        }
    }
}